/*******************************************************************************
 * Copyright (c) 2014 lifeng (lifengsofts@gmail.com)
 * See the file licenses/license.txt for copying permission.
 ******************************************************************************/
package cn.woblog.android.httputils.utils;

import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import android.provider.SyncStateContract.Constants;

import cn.woblog.android.httputils.domain.Request;
import cn.woblog.android.httputils.exception.AppException;
import cn.woblog.android.httputils.exception.AppException.Type;

public class HttpClientUtils {
	/**
	 * execute this http request
	 * 
	 * @author lifeng
	 * @return
	 * @throws Exception
	 */
	public static HttpResponse execute(Request request) throws AppException {
		switch (request.method) {
		case GET:
			return get(request);
		case POST:
			return post(request);
		case PUT:

			break;
		case DELETE:

			break;

		default:
			throw new IllegalStateException("the request method "
					+ request.method + " can't be support!");
		}
		return null;
	}

	private static HttpResponse get(Request request) throws AppException {
		HttpClient client = getClient();
		HttpGet httpGet = new HttpGet(request.url);
		setHeaders(httpGet, request.headers);
		try {
			return client.execute(httpGet);
		} catch (ClientProtocolException e) {
			throw new AppException(Type.ClientProtocolException, e.getMessage());
			// e.printStackTrace();
		} catch (IOException e) {
			throw new AppException(Type.IOException, e.getMessage());
			// e.printStackTrace();
		}
	}

	private static HttpClient getClient() {
		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT,
				Consts.CONNECTION_TIMEOUT);

		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				Consts.SO_TIMEOUT);
		return client;
	}

	private static HttpResponse post(Request request) throws AppException {
		HttpClient client = getClient();
		HttpPost httpPost = new HttpPost(request.url);
		if (request.entiry != null) {
			httpPost.setEntity(request.entiry);
		}
		setHeaders(httpPost, request.headers);
		try {
			return client.execute(httpPost);
		} catch (ClientProtocolException e) {
			// e.printStackTrace();
			throw new AppException(Type.ClientProtocolException, e.getMessage());
		} catch (IOException e) {
			// e.printStackTrace();
			throw new AppException(Type.IOException, e.getMessage());
		}
	}

	private static void setHeaders(HttpUriRequest httpUriRequest,
			Map<String, String> headers) {
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				httpUriRequest.addHeader(entry.getKey(), entry.getValue());
			}
		}
	}
}
